﻿using MediatR;
using Yuekao.API.Read.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.dto;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Read.CommandHandler
{
    public class GetDoctorDetailsHandler : IRequestHandler<GetDoctorDetailsCommand, APIPagging<DoctorDetailDto>>
    {
        private readonly IBaseRepository<Doctor> doctorRepo;
        private readonly IBaseRepository<DoctorDetails> doctorDetailRepo;
        private readonly IBaseRepository<Work> workRepo;
        private readonly IBaseRepository<Office> officeRepo;
        private readonly IBaseRepository<Hospital> hosRepo;

        public GetDoctorDetailsHandler(IBaseRepository<Doctor> doctorRepo,IBaseRepository<DoctorDetails> doctorDetailRepo, IBaseRepository<Work> workRepo, IBaseRepository<Office> officeRepo, IBaseRepository<Hospital> hosRepo)
        {
            this.doctorRepo = doctorRepo;
            this.doctorDetailRepo = doctorDetailRepo;
            this.workRepo = workRepo;
            this.officeRepo = officeRepo;
            this.hosRepo = hosRepo;
        }
        /// <summary>
        /// 医生详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIPagging<DoctorDetailDto>> Handle(GetDoctorDetailsCommand request, CancellationToken cancellationToken)
        {
            APIPagging<DoctorDetailDto> res=new APIPagging<DoctorDetailDto>();
            var doctor=doctorRepo.GetAll();
            var doctorDetail=doctorDetailRepo.GetAll();
            var work=workRepo.GetAll();
            var office=officeRepo.GetAll();
            var hos=hosRepo.GetAll();
            var linq = from a in doctorDetail
                       join b in doctor on a.DoctorId equals b.DoctorId
                       join c in work on b.WorkId equals c.WorkId
                       join d in office on b.OfficeId equals d.OfficeId
                       join e in hos on b.HospitalId equals e.HospitalId
                       select new DoctorDetailDto
                       {
                           WorkId = b.WorkId,
                           DoctorId = b.DoctorId,
                           WorkName = c.WorkName,
                           Begood = a.Begood,
                           CardNumber = a.CardNumber,
                           CardType = a.CardType,
                           Certificate = a.Certificate,
                           DetailsId = a.DetailsId,
                           HospitalId = e.HospitalId,
                           HospitalName = e.HospitalName,
                           IDCard = a.IDCard,
                           Introduction = a.Introduction,
                           JoinTime = b.JoinTime,
                           NickName = a.NickName,
                           OfficeId = b.OfficeId,
                           OfficeName = d.OfficeName,
                           PatientName = a.PatientName,
                       };
            res.PageData = linq.ToList();
            res.Data = res.PageData.FirstOrDefault(); // 设置Data属性为第一个元素或默认值
            res.Code = APIEnum.Success;
            return res;
        }
    }
}
